import 'dart:io';

import 'package:dio/dio.dart' as prefix;
import 'package:flutter/material.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:whale/helper/utils/media_picker.dart';

import 'httputils/http_man.dart';
import 'httputils/urlpath.dart';
import 'util.dart';

class UploadMan {
  Future<void> picker(
      {int max = 1, required Function(List<UploadResultModel>) result}) async {
    MediaPicker.pickImages(maxCount: max);
    // Popup.bottomSheet(
    //   data: ['相机', '相册'],
    //   onTap: (index) async {
    //     if (index < 2) {
    //       if (max == 1 || index == 0) {
    //         var file = await ImagePicker().pickImage(
    //             source: index == 0 ? ImageSource.camera : ImageSource.gallery,
    //             maxHeight: 667,
    //             maxWidth: 375,
    //             imageQuality: 50);
    //         if (file == null) {
    //           return;
    //         }
    //         var _url = await _upload([file!]);
    //         result(_url);
    //       } else {
    //         var _files = await ImagePicker().pickMultiImage(
    //             maxHeight: 667, maxWidth: 375, imageQuality: 50);
    //         if (_files.isEmpty) {
    //           result([]);
    //         }
    //         List<UploadResultModel> _urls = [];
    //         _urls = await _upload(_files);
    //         result(_urls);
    //       }
    //     }
    //   },
    // );
  }

  Future<List<UploadResultModel>> _upload(List<Media>? files) async {
    if (files == null || files.isEmpty) return [];

    List<prefix.MultipartFile> list = [];
    for (var e in files) {
      Image img = Image.file(File(e.path!));

      list.add(await prefix.MultipartFile.fromFile(e.path!,
          filename: Uri.parse(e.path!).authority));
    }
    Hud.show();

    var res = await HttpMan().upload(
      UrlPath.uploadFiles,
      data: prefix.FormData.fromMap(
        {
          'files': list,
        },
      ),
    );
    Hud.dismiss();
    List data = res.data;
    var results = data.map((e) => UploadResultModel.fromJson(e)).toList();

    return results;
  }
}

class UploadResultModel {
  String? id;
  String? name;
  dynamic duration;
  String? sourcePath;
  String? thumbnailPath;
  String? fileType;

  UploadResultModel({
    this.id,
    this.name,
    this.duration,
    this.sourcePath,
    this.thumbnailPath,
    this.fileType,
  });

  factory UploadResultModel.fromJson(Map<String, dynamic> json) {
    return UploadResultModel(
      id: json['id'] as String?,
      name: json['name'] as String?,
      duration: json['duration'] as dynamic,
      sourcePath: json['sourcePath'] as String?,
      thumbnailPath: json['thumbnailPath'] as String?,
      fileType: json['fileType'] as String?,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'duration': duration,
        'sourcePath': sourcePath,
        'thumbnailPath': thumbnailPath,
        'fileType': fileType,
      };
}
